<?php
require_once 'models/models_loaihang.php';
$mod = $_GET['mod'];
$oblh = new models_loaihang();
switch ($mod)
{
	case 'loaihang':{
		$data['listtrang']=$oblh->listrang($_POST['start'],$_POST['display'] );
		$data['sodong']=$oblh->sodong();
		$data['list'] = $oblh->listloaihang();
		$display = 5;
            // tinh tong so trang can hien thi
    		if(isset($_GET['trang']) && (int)$_GET['trang']) {
                $trang = $_GET['trang'];
            } else { //neu chua xac dinh, thi tim so trang
                if($data['sodong'] > $display) {
                    $trang = ceil($data['sodong']/$display);
                } else {
                    $trang = 1;
                }
            }
            $start = (isset($_GET['start']) && (int)$_GET['start']>=0) ? $_GET['start'] : 0;
            
		require 'views/admin/loaihang/index.php';
		break;
	}
	case 'addloaihang':{
		if($_POST)
		{
			$oblh->_addloahang($_POST['tenlh']);	
			echo '<script> window.location.href="admin.php?mod=loaihang"</script>';
		}
		require 'views/admin/loaihang/add.php';
		break;
	}
	case 'delloaihang':{
		$oblh->del_loaihang($_GET['ma']);
		echo '<script> window.location.href="admin.php?mod=loaihang"</script>';
		break;
	}
	case 'updateloaihang':{
		if($_POST){
				$oblh->updateLoaiHang($_POST['tenlh'], $_GET['ma']);
				echo '<script>window.location.href="admin.php?mod=loaihang"</script>';
		}
		$data = $oblh->getLoaiHang($_GET['ma']);
		require 'views/admin/loaihang/edit.php';
		break;
	}
	default: echo 'Khong tim thay yeu cau !';
		break ;
}
?>